/**
 * @(#)FileUtils.java
 * Copyright (C) 2003-2010 iocn.org All rights reserved
 */
package org.iocn.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


/**
 * 文件相关操作工具类
 * 
 * @author <b>Oxidy</b>, Copyright &#169; 2010
 * @version 0.1,2010-6-8
 */
public class FileUtils {
    
    /**
     * 获取文件扩展名
     * @param fileName
     *            文件全名
     * @return 文件扩展名
     * @modify lifw20080401
     */
    public static String getFileExt(String fileName) {
        String fileExt = null;
        int i = 0;
        int j = 0;
        if (fileName == null) {
            return "";
        }
        fileName = fileName.replace('\\', '/');
        i = fileName.lastIndexOf('/') + 1;
        j = fileName.length();
        if (i >= 0) {
            fileName = fileName.substring(i, j);
        }
        i = fileName.lastIndexOf('.') + 1;
        j = fileName.length();
        if (i >= 0) {
            fileExt = fileName.substring(i, j);
            return fileExt;
        } else {
            return "";
        }
    }
    
    /**
     * 获取文件名
     * @param fileName
     *            文件全名
     * @return 获得文件名
     * @modify lifw20080401
     */
    public static String getFileName(String fileName) {
        int i = 0;
        int j = 0;
        if (fileName == null) {
            return "";
        }
        fileName = fileName.replace('\\', '/');
        i = fileName.lastIndexOf('/') + 1;
        j = fileName.length();
        if (i >= 0) {
            return fileName.substring(i, j);
        } else {
            return "";
        }
    }
    
    /**
     * 将文件从源位置srFilePath拷贝到目标位置dtFilePath
     * 
     * @param srFilePath
     *            源位置
     * @param dtFilePath
     *            目标位置
     * @author zhangyj
     * @version v0.1,2010/01/05
     */
    public static void copyFile(String srFilePath, String dtFilePath) {
        try {
            File srFile = new File(srFilePath);
            File dtFile = new File(dtFilePath);

            InputStream in = new FileInputStream(srFile);
            OutputStream out = new FileOutputStream(dtFile);

            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 文件拷贝存储
     * @param src
     *          原来文件
     * @param dst
     *          目标文件
     * @author Oxidy
     * @version 0.1,2010-06-30
     */
    public static void copyFile(final File src, final File dst) {
        try {
            final int BUFFER_SIZE = 16 * 1024;
            InputStream is = null;
            OutputStream os = null;
            try {
                is = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
                os = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
                byte[] buffer = new byte[BUFFER_SIZE];
                while (is.read(buffer) > 0) {
                    os.write(buffer);
                }
            } finally {
                if (null != is) {
                    is.close();
                }
                if (null != os) {
                    os.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
